<?php

defined('SYSPATH') or die('Access error!');

/**
 *  用户的积分处理类
 */
class Model_User_Points extends Model_User_Account
{
   
    /**
     * 获取积分的配置
     */
    public function get_points_config()
    {
        return $this->get_account_config(USER_ACCOUNT_POINTS);
    }

    /**
     * 获取积分的系统设置
     * @return type
     */
    public function get_points_setting()
    {
        return $this->get_account_setting(3);
    }

    /**
     * 处理积分的配置和系统的设置
     * @param type $code
     * @return type
     */
    public function handle_points_config_setting($code)
    {
        $this->account_config = $this->get_points_config();
        $this->account_setting = $this->get_points_setting();
        return $this->handle_account_config_setting($code);
    }

    /**
     * 根据用户的id获得某用户的当前积分
     * @param type $user_id
     * @return type
     */
    public function get_points($user_id)
    {
        $user_points = $this->get_user_account($user_id, USER_ACCOUNT_POINTS);
        return $user_points[USER_ACCOUNT_POINTS];
    }

    /**
     *  保存积分和日志
     * @param type $to_user_id
     * @param type $code
     * @param type $detail_id
     * @param type $from_user_id
     * @return boolean
     */
    public function save_points($to_user_id, $code, $detail_id, $from_user_id = 0)
    {
        $this->to_user_id = $to_user_id;
        //根据id获取用户当前的积分
        $this->current_account = $this->get_points($to_user_id);
        $this->from_user_id = $from_user_id;
        $points_config = $this->handle_points_config_setting($code);
        $this->message = $points_config['message'];
        $this->account = $points_config['account'];
        $this->time = $points_config['time'];
        $this->operation = $points_config['operation'];
        $this->type = USER_ACCOUNT_POINTS;
        $this->detail_id = $detail_id;
        //更新用户的积分
        if ($this->update_account())
        {
            //更新用户日志
            $this->update_account_log();
            return TRUE;
        }
    }

}